package com.wing.bill.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.bill.entity.PurchaseBill;
import com.wing.bill.service.PurchaseBillService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 采购账单
 *
 * @author xiayang
 */
@Validated
@Api(value = "采购账单管理", tags = "#/purchase/purchaseBill")
@RestController("purchaseBillController")
@RequestMapping("purchase/admin/purchaseBill")
public class PurchaseBillController {

    @Resource
    private PurchaseBillService purchaseBillService;

    @ApiOperation(value = "查询采购账单列表", notes = "列表")
    @GetMapping("/list")
    public PageResult<PurchaseBill> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            String settleStatus, String receiptStatus, String code, String supplierName, String billDate) {
        Page<PurchaseBill> pg = new Page<>(page, limit);
        Map paramMap = new HashMap();
        paramMap.put("settleStatus", settleStatus);
        paramMap.put("receiptStatus", receiptStatus);
        paramMap.put("code", code);
        paramMap.put("supplierName", supplierName);
        paramMap.put("billDate", billDate);
        IPage<PurchaseBill> list = purchaseBillService.pageList(pg, paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "账单详细", notes = "详情")
    @GetMapping("/get")
    public JsonResult get(@RequestParam Long id) {
        return JsonResult.success(purchaseBillService.get(id));
    }


    @ApiOperation(value = "添加账单", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@RequestBody PurchaseBill purchaseBill) {
        purchaseBillService.add(purchaseBill);
        return JsonResult.success("添加成功");
    }

    @ApiOperation(value = "账单审核", notes = "审核")
    @GetMapping("/audit")
    public JsonResult audit(@RequestParam Long id) {
        PurchaseBill purchaseBill = purchaseBillService.getById(id);
        purchaseBill.setSettleStatus("2");
        purchaseBill.setModifier(JwtUtils.getLoginUserId());
        purchaseBill.setModifierName(JwtUtils.getLoginUsername());
        purchaseBill.setUpdateTime(LocalDateTime.now());
        purchaseBillService.updateById(purchaseBill);
        return JsonResult.success("审核成功");
    }

}
